/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RadioProp1546;

/**
 *
 * @author Alwyn
 */

public class PropagationCalcs {

    public static final int LAND=0;
    public static final int COLD_SEA=1;
    public static final int WARM_SEA=2;

    private RefractivityMap refract=new RefractivityMap("c:");
    private PropCurves propCurves=new PropCurves("c:");

    /**
     *
     * Calculates the basic transmission loss
     *
     * @param E the field strength in dB-uV/m for 1kW erp
     * @param f the frequency in MHz
     * @return basic transmission loss in dB
     * */
    public static double Lb(double E, double f)
    {
        return 139.3-E+20*Math.log10(f);
    }

    /**
     *
     * An approximation of the 0.6 Fresnel clearance path length.
     *
     * @param h1 the height of antenna 1 in metres
     * @param h2 the height of antenna 2 in metres
     * @param f the frequency in MHz
     * @return path length in km.
     * */
    public static double D06(double h1, double h2, double f)
    {
        if(h1<0)
            h1=0;

        double Df=0.0000389*f*h1*h2;
        double Dh=4.1*(Math.sqrt(h1)+Math.sqrt(h2));

        double ret=(Df*Dh)/(Df+Dh);

        if(ret<0.001)
            ret=0.001;

        return ret;
    }

    /**
     * Calculates the maximum field strength of a path
     *
     * @param d distance between transmitter and receiver in km
     * @param percent percentage of the time needed in % (not decimal e.g 0.5)
     * @param surface the type of surface the signal is travelling over
     * @return the maximum field strength in dB-uV/m for 1kW transmitter.
     */
    public static double maxFieldStrength(double d, double percent, int surface)
    {
        double Efs=106.9-20*Math.log10(d);

        if(surface==LAND)
            return Efs;

        double Ese=2.38*(1-Math.exp(-d/8.94))*Math.log10(50/percent);

        return Efs+Ese;
    }

    /**
     * Calculates the field strength for sea paths with frequency less than 100MHz.
     * 
     * @param f frequency in MHz
     * @param h1 transmitter height in metres
     * @param d distance between transmitter and receiver in km
     * @param percent percentage required in % (not decimal, e.g. 0.5)
     * @return the field strength in dB-uV/m for 1kW transmitter
     */
    public double sea100MHz(double f, double h1, double d, double percent)
    {
        double df=D06(f,h1,10);

        if(d<=df)
            return maxFieldStrength(d,percent,COLD_SEA);

        double Edf=maxFieldStrength(df,percent,COLD_SEA);
        double d600=D06(600,h1,10);
        double Ed600=propCurves.getFieldStrength(d600, h1, percent, PropCurves.COLD_SEA);

        return Edf+(Ed600-Edf)*Math.log10(d/df)/Math.log10(d600/df);
    }
}
